package com.xfunc.ok.dmok;

/**
 * 进度回调
 */
public abstract class ProgressListener implements ProgressCallback {
    boolean started;
    long lastRefreshTime = 0L;
    long lastBytesWritten = 0L;
    int minTime = 500;//最小回调时间100ms，避免频繁回调
    long startTime = System.currentTimeMillis();
    String url = "";
    /**
     * 进度发生了改变，如果numBytes，totalBytes，percent都为-1，则表示总大小获取不到
     *
     * @param numBytes   已读/写大小
     * @param totalBytes 总大小
     * @param percent    百分比
     */
    public final void onProgressChanged(long numBytes, long totalBytes, float percent) {
        if (!started) {
            onProgressStart(totalBytes);
            onProgressStart(url,totalBytes);
            startTime = System.currentTimeMillis();
            started = true;
        }
        if (numBytes == -1 && totalBytes == -1 && percent == -1) {
            onProgressChanged(-1, -1, -1, -1);
            return;
        }
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastRefreshTime >= minTime || numBytes == totalBytes || percent >= 1F) {
            long intervalTime = (currentTime - lastRefreshTime);
            if (intervalTime == 0) {
                intervalTime += 1;
            }
            long updateBytes = numBytes - lastBytesWritten;
            final long networkSpeed = updateBytes / intervalTime;
            onProgressChanged(numBytes, totalBytes, percent, networkSpeed);
            onProgressChanged(url,numBytes, totalBytes, percent, networkSpeed);
            lastRefreshTime = System.currentTimeMillis();
            lastBytesWritten = numBytes;
        }
        if (numBytes == totalBytes || percent >= 1F) {
            onProgressFinish();
            onProgressFinish(url,totalBytes,startTime);
        }
    }

    /**
     * 进度发生了改变，如果numBytes，totalBytes，percent，speed都为-1，则表示总大小获取不到
     *
     * @param numBytes   已读/写大小
     * @param totalBytes 总大小
     * @param percent    百分比
     * @param speed      速度 bytes/ms
     */
    public  void onProgressChanged(long numBytes, long totalBytes, float percent, float speed){

    }
    public  void onProgressChanged(String url,long numBytes, long totalBytes, float percent, float speed){

    }
    /**
     * 进度开始
     *
     * @param totalBytes 总大小
     */
    public void onProgressStart(long totalBytes) {

    }
    public void onProgressStart(String url,long totalBytes) {

    }
    /**
     * 进度结束
     */
    public void onProgressFinish() {

    }
    /**
     * 进度结束
     */
    public void onProgressFinish(String url,long totalBytes,long startTime) {

    }

    /**
     * 重复下载
     * @param url
     */
    public void onRepeat(String url){
        System.out.println("重复下载。。。"+url);
    }

    /**
     * 准备
     * @param url
     */
    public void onReady(String url){
        this.url = url;
        System.out.println("准备工作完成 开始下载。。。"+url);
    }
    /**
     * 准备
     * @param url
     */
    public void onError(String url,String msg){
        this.url = url;
        System.out.println("下载出错了："+url+" ，错误："+msg);
    }
    /**
     * 是否支持断点续传功能
     * @param isBreakpointContinuation
     */
    public void onSupportBreakpointContinuation(String url,boolean isBreakpointContinuation){
        System.out.println(url+" 是否支持断点续传功能： "+isBreakpointContinuation);
    }
    /**
     * 取消下载
     * @param url
     */
    public void cancel(String url){
        System.out.println(" 取消下载。。。"+url);
    }
}
